$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'base/workergroup/list',
        datatype: "json",
        colModel: [			
			{ label: '序号', name: 'id', index: 'ID', width: 30, key: true },
			{ label: '姓名', name: 'name', index: 'NAME', width: 60 }, 			
			{ label: '班组', name: 'cgroup', index: 'CGROUP', width: 50 }, 			
			{ label: '身份证号码', name: 'idcard', index: 'IDCARD', width: 90 }, 			
			{ label: '工号', name: 'workid', index: 'WORKID', width: 40 }, 			
			{ label: '性别', name: 'sex', index: 'SEX', width: 40 }, 			
			{ label: '电话', name: 'tel', index: 'TEL', width: 80 }, 			
			{ label: '创建人', name: 'cuser', index: 'CUSER', width: 60 }, 			
			{ label: '创建时间', name: 'ctime', index: 'CTIME', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	// 隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		workergroup: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.workergroup = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.workergroup.id == null ? "base/workergroup/save" : "base/workergroup/update";
			vm.workergroup.cgroup = $("#cgroup").val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.workergroup),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "base/workergroup/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "base/workergroup/info/"+id, function(r){
				vm.workergroup = r.workergroup;
				//console.log(vm.workergroup.cgroup);
				//console.log($("#grouper").find("option[text='"+r.workergroup.cgroup+"']"));
				
				$("#cgroup option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            //console.log(optionVal +" " + optionText);
		            if(optionText == vm.workergroup.cgroup )
		            {
		            	$("#cgroup").val(optionVal)
		            }
		        });
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

$(function(){ 
	$.ajax({
		type: "GET",
	    url: baseURL + 'base/group/qlist'+'?page=1&limit=100',
        contentType: "application/json",
	    data: '',
	    success: function(r){
	    	for(var i in r.groupList){  
	    		   $("#cgroup").append('<option value='+r.groupList[i].id+'>' + r.groupList[i].name + '</option>');
	    		} 
		}
	});
	}
);
	
	
		
	　　

